#!/bin/bash

echo "=========================================="
echo "Elasticsearch 自动补全学习项目启动脚本"
echo "=========================================="

# 检查 Java 环境
if ! command -v java &> /dev/null; then
    echo "❌ 错误: 未找到 Java 环境，请先安装 Java 8+"
    exit 1
fi

# 检查 Maven 环境
if ! command -v mvn &> /dev/null; then
    echo "❌ 错误: 未找到 Maven 环境，请先安装 Maven 3.6+"
    exit 1
fi

# 检查 Elasticsearch 是否运行
echo "🔍 检查 Elasticsearch 连接..."
if curl -s http://117.72.38.248:19200 > /dev/null; then
    echo "✅ Elasticsearch 连接正常"
else
    echo "❌ 错误: 无法连接到 Elasticsearch (http://117.72.38.248:19200)"
    echo "请确保 Elasticsearch 已启动，可以使用以下命令启动："
    echo "docker run -d --name elasticsearch -p 19200:9200 -e \"discovery.type=single-node\" -e \"xpack.security.enabled=false\" elasticsearch:7.17.9"
    exit 1
fi

# 编译项目
echo "🔨 编译项目..."
mvn clean compile

if [ $? -ne 0 ]; then
    echo "❌ 编译失败"
    exit 1
fi

echo "✅ 编译成功"

# 启动应用
echo "🚀 启动应用..."
echo "应用将在 http://localhost:8080 启动"
echo "按 Ctrl+C 停止应用"
echo "=========================================="

mvn spring-boot:run
